﻿using Models;
using NewTest.Dao;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SqlSugar;


namespace SoonSheng.BLL
{
    public class Headbll
    {
        /// <summary>
        /// 获取主图切换
        /// </summary>
        /// <returns></returns>
        public List<tbl_main_img> GetMainImgList()
        {
            using (var db = SugarDao.GetInstance())
            {
                List<tbl_main_img> imglist=db.Queryable<tbl_main_img>().Where(s => s.type == 2 && s.isdel==false).ToList();
                return imglist;
            }
        }
        public tbl_main_img GetLgoImg()
        {
            using (var db = SugarDao.GetInstance())
            {
                tbl_main_img imglogo = db.Queryable<tbl_main_img>().Where(s => s.type == 1 && s.isdel == false).FirstOrDefault();
                return imglogo;
            }
        }
        public List<tbl_product> GetProductImgList()
        {
            using (var db = SugarDao.GetInstance())
            {
                List<tbl_product> prodimg = db.Queryable<tbl_product>().Where(c => c.prod_id > 0 && c.isdel == false).OrderBy(c => c.createtime,OrderByType.Desc).Skip(0).Take(8).ToList();
                return prodimg;
            }
        }
        public tbl_product GetProductByID(int prod_id)
        {
            using (var db = SugarDao.GetInstance())
            {
                tbl_product prod = db.Queryable<tbl_product>().Where(c => c.prod_id == prod_id && c.isdel == false).FirstOrDefault();
                return prod;
            }
        }
        public List<tbl_prod_class> GetProductClass()
        {
            using (var db = SugarDao.GetInstance())
            {
                List<tbl_prod_class> prodclass = db.Queryable<tbl_prod_class>().Where(c => c.isdel == false).ToList();
                return prodclass;
            }
        }
        
        public List<tbl_product> GetProductListByClassID(int class_id)
        {
            using (var db = SugarDao.GetInstance())
            {
                List<tbl_product> prodimg = db.Queryable<tbl_product>().Where(c => c.prod_class_id ==class_id&& c.isdel==false).ToList();
                return prodimg;
            }
        }

        public List<tbl_product> GetALLProductList()
        {
            using (var db = SugarDao.GetInstance())
            {
                List<tbl_product> prodimg = db.Queryable<tbl_product>().Where(c => c.isdel == false).ToList();
                return prodimg;
            }
        }

        public List<tbl_companyinfo_type> GetALLCompanyInfotype()
        {
            using (var db = SugarDao.GetInstance())
            {
                List<tbl_companyinfo_type> companyinfotype = db.Queryable<tbl_companyinfo_type>().ToList();
                return companyinfotype;
            }
        }

        public tbl_companyinfo_detail GetALLCompanyInfotype(int type_id)
        {
            using (var db = SugarDao.GetInstance())
            {
                tbl_companyinfo_detail companyinfotypedetail = db.Queryable<tbl_companyinfo_detail>().Where(s => s.info_typeID == type_id).FirstOrDefault();
                return companyinfotypedetail;
            }
        }

        public List<tbl_news> GetNewList()
        {
            using (var db = SugarDao.GetInstance())
            {
                List<tbl_news> newslist = db.Queryable<tbl_news>().Where(c => c.news_id > 0 && c.isdel == false).OrderBy(c => c.createtime, OrderByType.Desc).Skip(0).Take(3).ToList();
                for (int i=0;i<newslist.Count;i++)
                {
                    newslist[i].news_body = newslist[i].news_body.ToString().Substring(0,20)+"...";
                }
                return newslist;
            }
        }

        public List<tbl_news_class> GetALLNewType()
        {
            using (var db = SugarDao.GetInstance())
            {
                List<tbl_news_class> newclass = db.Queryable<tbl_news_class>().ToList();
                return newclass;
            }
        }

        public List<tbl_news> GetALLNews()
        {
            using (var db = SugarDao.GetInstance())
            {
                List<tbl_news> newslist = db.Queryable<tbl_news>().Where(s => s.isdel == false).ToList();
                for (int i = 0; i < newslist.Count; i++)
                {
                    newslist[i].news_body = newslist[i].news_body.ToString().Substring(0, 20) + "...";
                }
                return newslist;
            }
        }
        public List<tbl_news> GetALLNewsByClassID(int newclass_id)
        {
            using (var db = SugarDao.GetInstance())
            {
                List<tbl_news> newslistclass = db.Queryable<tbl_news>().Where(s => s.isdel == false && s.news_id== newclass_id).ToList();
                for (int i = 0; i < newslistclass.Count; i++)
                {
                    newslistclass[i].news_body = newslistclass[i].news_body.ToString().Substring(0, 20) + "...";
                }
                return newslistclass;
            }
        }

        public tbl_news GetNewsDeatilByNewsID(int news_id)
        {
            using (var db = SugarDao.GetInstance())
            {
                tbl_news newsdetail = db.Queryable<tbl_news>().Where(s => s.isdel == false && s.news_id == news_id).FirstOrDefault();
                return newsdetail;
            }
        }
    }
}